package net.sansi.v3correctserver.util;

import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sansi.v3correctserver.exception.ServiceException;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by Fang
 * create time : 2025/1/22
 * IDE : IntelliJ IDEA
 * <p>
 * 基于 SpringBoot 的 接送工具分装
 */

public class JacksonUtil {

    /**
     * 获取object对象
     *
     * @return ObjectMapper
     */
    public static ObjectMapper getObjectMapper() {
        return SpringUtil.getBean(ObjectMapper.class);
    }

    /**
     * json 转对象
     *
     * @param json  json字符串
     * @param clazz 对象类型
     * @return 对象
     */
    public static <T> T readValue(String json, Class<T> clazz) {
        try {
            return getObjectMapper().readValue(json, clazz);
        } catch (Exception e) {
            throw new ServiceException("json解析失败", e);
        }
    }

    public static <T> T readValue(File file, Class<T> clazz) {
        try {
            return getObjectMapper().readValue(file, clazz);
        } catch (Exception e) {
            throw new ServiceException("json解析失败", e);
        }
    }

    /**
     * json 转对象列表
     *
     * @param json  json字符串
     * @param clazz 内容对象
     * @return 列表
     */
    public static <T> List<T> readList(String json, Class<T> clazz) {
        try {
            return getObjectMapper().readerForListOf(clazz).readValue(json);
        } catch (Exception e) {
            throw new ServiceException("json解析失败", e);
        }
    }

    public static <T> List<T> readList(File file, Class<T> clazz) {
        try {
            return getObjectMapper().readerForListOf(clazz).readValue(file);
        } catch (Exception e) {
            throw new ServiceException("json解析失败", e);
        }
    }

    /**
     * json 转 map
     *
     * @param json  json字符串
     * @param clazz 内容对象
     * @return Map
     */
    public static <T> Map<String, T> readMap(String json, Class<T> clazz) {
        try {
            return getObjectMapper().readerForMapOf(clazz).readValue(json);
        } catch (Exception e) {
            throw new ServiceException("json解析失败", e);
        }
    }

    public static <T> Map<String, T> readMap(File file, Class<T> clazz) {
        try {
            return getObjectMapper().readerForMapOf(clazz).readValue(file);
        } catch (Exception e) {
            throw new ServiceException("json解析失败", e);
        }
    }

    /**
     * 对象转json
     *
     * @param obj 对象
     * @return json字符串
     */
    public static String toString(Object obj) {
        try {
            return getObjectMapper().writeValueAsString(obj);
        } catch (Exception e) {
            throw new ServiceException("json序列化失败", e);
        }
    }

    /**
     * 对象转json
     * 格式化
     *
     * @param obj 对象
     * @return json字符串
     */
    public static String toPrettyString(Object obj) {
        try {
            return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (Exception e) {
            throw new ServiceException("json序列化失败", e);
        }
    }


}
